package marchsoft.modules.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import marchsoft.base.BasicServiceImpl;
import marchsoft.enums.ResultEnum;
import marchsoft.exception.BadRequestException;
import marchsoft.modules.system.entity.DictTYpe;
import marchsoft.modules.system.entity.DictValue;
import marchsoft.modules.system.mapper.DictTypeMapper;
import marchsoft.modules.system.mapper.DictValueMapper;
import marchsoft.modules.system.service.IDictService;
import marchsoft.response.Result;
import marchsoft.utils.SecurityUtils;
import org.springframework.stereotype.Service;

import java.util.Set;

@Slf4j
@Service
@RequiredArgsConstructor
public class IDictServiceImpl extends BasicServiceImpl<DictValueMapper, DictValue> implements IDictService {

    private final DictTypeMapper dictTypeMapper;

    private final DictValueMapper dictValueMapper;
    @Override
    public DictTYpe findById(Long id) {
        DictTYpe dictTYpe = dictTypeMapper.selectById(id);
        if (ObjectUtil.isEmpty(dictTYpe)) {
            log.error(StrUtil.format("【查找字典失败】操作人id：{}，查找目标id：{}", SecurityUtils.getCurrentUserId(), id));
            throw new BadRequestException(ResultEnum.DATA_NOT_FOUND);
        }
        return dictTYpe;
    }

    @Override
    public Result<Object> findByAllDictType(String typeName, Page page) {
        LambdaQueryWrapper<DictTYpe> wrapper = new LambdaQueryWrapper<>();
        // 名称
        if (ObjectUtil.isNotNull(typeName)) {
            wrapper.like(DictTYpe::getDictTypeName, typeName);
        }
        return  Result.success(dictTypeMapper.selectPage(page,wrapper));
    }

    @Override
    public Result<Object> findByTypeId(Long typeId, Page page) {
        return Result.success(dictValueMapper.selectDictValuePage(page,typeId));
    }

    @Override
    public Result<Object> updateDictType(DictTYpe dictTYpe) {
        if(dictTypeMapper.updateById(dictTYpe) > 0){
            return Result.success("更新成功！");
        };
        return Result.error("更新失败！");
    }

    @Override
    public Result<Object> updateDictValue(DictValue dictValue) {
        if(dictValueMapper.updateById(dictValue) > 0){
            return Result.success("更新成功！");
        };
        return Result.error("更新失败！");
    }

    @Override
    public Result<Object> deleteDictType(Set<Long> ids) {
        if(dictTypeMapper.deleteBatchIds(ids) > 0){
            return Result.success("删除成功！");
        };
        return Result.error("删除失败！");
    }

    @Override
    public Result<Object> deleteDictValue(Set<Long> ids) {
        if(dictValueMapper.deleteBatchIds(ids) > 0){
            return Result.success("删除成功！");
        };
        return Result.error("删除失败！");
    }

    @Override
    public Result<Object> createDictType(DictTYpe dictTYpe) {
        if(dictTypeMapper.insert(dictTYpe)>0){
            return Result.success("创建成功！");
        }
        return Result.error("创建失败");
    }

    @Override
    public Result<Object> createDictValue(DictValue dictValue) {
        if(dictValueMapper.insert(dictValue)>0){
            return Result.success("创建成功！");
        }
        return Result.error("创建失败！");
    }
}
